package com.example.demo.controller;

import com.example.demo.entity.ModelEntity;
import com.example.demo.util.ExcelUtil;
import com.example.demo.util.OSSUtil;
import com.example.demo.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
@CrossOrigin
@Slf4j
public class TestController {

    @Resource
    private RestTemplate restTemplate;
    @Value("${fangyi.url}")
    private String url;

    @PostMapping("upload")
    public String upload(@RequestParam MultipartFile file){
        log.info("接收到上传图片请求");
        String url = OSSUtil.upload(file, OSSUtil.FileDirType.AVATAR);
        log.info("返回url："+url);
        return url;
    }

    @PostMapping("uploadStudent/excel")
    public Map uploadStudent(@RequestParam MultipartFile file){
        log.info("接收到导入学生请求");
        List<ModelEntity> entities = ExcelUtil.importExcel(file, ModelEntity.class, null);
        ResponseEntity<Map> responseEntity = RestTemplateUtil.sendPostForEntity(restTemplate,
                url,
                entities,
                Map.class);
        return responseEntity.getBody();
    }
}
